import React from 'react'
import { hasToken } from '@/utils/storage'
import { Redirect, Route, RouteProps, useLocation } from 'react-router-dom'

export default function PrivateRoute({ children, ...res }: RouteProps) {
  const location = useLocation()
  return (
    <Route
      {...res}
      render={(): any =>
        hasToken() ? (
          children
        ) : (
          <Redirect
            to={{ pathname: '/login', state: { from: location.pathname } }}
          ></Redirect>
        )
      }
    ></Route>
  )
}
